/*
 * Copyright (c) 1999-2003, Carnegie Mellon University. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * 3. Products derived from the software may not be called "iVProg",
 *    nor may "iVProg" appear in their name, without prior written
 *    permission of Carnegie Mellon University.
 * 
 * 4. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    "This product includes software developed by Carnegie Mellon University"
 */

package edu.cmu.cs.stage3.alice.core.response;

import edu.cmu.cs.stage3.alice.core.property.ElementArrayProperty;

public abstract class CompositeResponse extends edu.cmu.cs.stage3.alice.core.Response {

 public final ElementArrayProperty componentResponses = new ElementArrayProperty( this, "componentResponses", null, edu.cmu.cs.stage3.alice.core.Response[].class );
 protected static RuntimeResponse[] s_emptyRuntimeResponses = new RuntimeResponse[ 0 ];

static int cont=0;

 // public abstract class RuntimeCompositeResponse extends RuntimeResponse - edu/cmu/cs/stage3/alice/core/Response$RuntimeResponse.class
 public abstract class RuntimeCompositeResponse extends RuntimeResponse {
  private RuntimeResponse[] m_runtimeResponses = null;

// at edu.cmu.cs.stage3.alice.core.response.CompositeResponse$RuntimeCompositeResponse.manufactureComponentRuntimeResponses(CompositeResponse.java:40)
// at edu.cmu.cs.stage3.alice.core.response.CompositeResponse$RuntimeCompositeResponse.getRuntimeResponses(CompositeResponse.java:67)
// at edu.cmu.cs.stage3.alice.core.response.DoInOrder$RuntimeDoInOrder.getChildCount(DoInOrder.java:51)
// at edu.cmu.cs.stage3.alice.core.response.DoInOrder$RuntimeDoInOrder.isCullable(DoInOrder.java:84)
// at edu.cmu.cs.stage3.alice.core.response.DoInOrder$RuntimeDoInOrder.update(DoInOrder.java:112)
// at edu.cmu.cs.stage3.alice.core.response.CallToUserDefinedResponse$RuntimeCallToUserDefinedResponse.update(CallToUserDefinedResponse.java:93)
// at edu.cmu.cs.stage3.alice.core.behavior.TriggerBehavior.internalSchedule(TriggerBehavior.java:55)
// at edu.cmu.cs.stage3.alice.core.Behavior.schedule(Behavior.java:232)
// at edu.cmu.cs.stage3.alice.core.Sandbox.scheduleBehaviors(Sandbox.java:62)
// at edu.cmu.cs.stage3.alice.core.World.schedule(World.java:199)
// at edu.cmu.cs.stage3.alice.core.clock.DefaultClock.schedule(DefaultClock.java:72)
// at edu.cmu.cs.stage3.alice.authoringtool.AuthoringTool$5.run(AuthoringTool.java:427)
// at edu.cmu.cs.stage3.alice.authoringtool.util.DefaultScheduler.simulateOnce(DefaultScheduler.java:85)
// at edu.cmu.cs.stage3.alice.authoringtool.util.DefaultScheduler.run(DefaultScheduler.java:50)
// at edu.cmu.cs.stage3.scheduler.AbstractScheduler.run(AbstractScheduler.java:95)
// at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

  protected RuntimeResponse[] manufactureComponentRuntimeResponses (ElementArrayProperty property) {
//if (cont==0) try{String str=""; System.out.println(str.charAt(3));}catch(Exception e){e.printStackTrace();}
   RuntimeResponse[] runtimeResponses = new RuntimeResponse[ property.size() ];
System.out.print("CompositeResponse.java: "+property.size()+" <");
   int actualRuntimeResponseCount = 0;
   for (int i=0; i<property.size(); i++) {
    edu.cmu.cs.stage3.alice.core.Response response = (edu.cmu.cs.stage3.alice.core.Response)property.get( i );
    if (response != null) {
System.out.print(property.getName()+",");
     runtimeResponses[actualRuntimeResponseCount] = response.manufactureRuntimeResponse();
     actualRuntimeResponseCount++;
     }
else System.out.print("(),");
    }
System.out.println(">");
//try {String str=""; System.out.println(str.charAt(3));}catch(Exception e) {e.printStackTrace();}

   if (actualRuntimeResponseCount<runtimeResponses.length) {
    RuntimeResponse[] trimmedRuntimeResponses = new RuntimeResponse[ actualRuntimeResponseCount ];
    System.arraycopy( runtimeResponses, 0, trimmedRuntimeResponses, 0, actualRuntimeResponseCount );
    return trimmedRuntimeResponses;
   } else {
    return runtimeResponses;
    }
   }

  protected RuntimeResponse[] getRuntimeResponses () {
   if (m_runtimeResponses == null) {
    m_runtimeResponses = manufactureComponentRuntimeResponses( componentResponses );
    }
   return m_runtimeResponses;
   }

  protected void childrenEpiloguesIfNecessary (double t) {
   RuntimeResponse[] runtimeResponses = getRuntimeResponses();
   //L for (RuntimeResponse runtimeResponse : runtimeResponses)
   int t_tam = runtimeResponses!=null ? runtimeResponses.length : 0;
   for (int i_i=0; i_i<t_tam; i_i++) {
    if (runtimeResponses[i_i].isActive()) { //L runtimeResponse.isActive()
       runtimeResponses[i_i].epilogue( t ); //L runtimeResponse.epilogue( t )
       }
    }
   }

  //Rem @Override
  public void epilogue( double t) {
   super.epilogue( t );
   childrenEpiloguesIfNecessary( t );
   }
  }

 }
